package DAO;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import hibernate.AssetDamageInfo;
import hibernate.AssetInfo;
import hibernateUtil.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZCBS {
	
	private int totalByQuery = 0;

	@SuppressWarnings("unchecked")
	/*
	 * 得到所有的资产编号
	 * assetNo
	 */
	public List getAllAssetNo()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		List list = session.createQuery("select assetNo from AssetInfo").list();
		
		trans.commit();
		
		return list;
	}
	
	/*
	 * 通过资产编号得到资产信息
	 * 参数 -- 资产编号(assetNo)
	 */
	public AssetInfo getAsset(String assetNo)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetInfo assetInfo = (AssetInfo)session.get(AssetInfo.class, assetNo);
		
		trans.commit();
		
		return assetInfo;
	}
	
	/*
	 * save资产报损信息
	 */
	public void addAssetBs(String parameter[])
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetDamageInfo assetDamageInfo = new AssetDamageInfo();
		
		Date AppraisalTime = java.sql.Date.valueOf(parameter[5]);
		Date startUseTime = java.sql.Date.valueOf(parameter[9]);
		double money = Double.parseDouble(parameter[7]);
		int lifeYears = Integer.parseInt(parameter[3]);
		
		assetDamageInfo.setAssetNo(parameter[0]);
		assetDamageInfo.setModel(parameter[1]);
		assetDamageInfo.setDepartment(parameter[2]);
		assetDamageInfo.setLifeYears(lifeYears);		
		assetDamageInfo.setAppraisaler(parameter[4]);
		assetDamageInfo.setAppraisalTime(AppraisalTime);
		assetDamageInfo.setName(parameter[6]);
		assetDamageInfo.setMoney(money);		
		assetDamageInfo.setPrincipal(parameter[8]);
		assetDamageInfo.setStartUseTime(startUseTime);
		assetDamageInfo.setNowStatus(parameter[10]);
		assetDamageInfo.setDamageReason(parameter[11]);
		assetDamageInfo.setRemark(parameter[12]);
		assetDamageInfo.setAppraisalOpinion(parameter[13]);
		assetDamageInfo.setApproveOpinion("未审批");
		
		session.save(assetDamageInfo);
		trans.commit();		
	}
	
    /*
     * 得到资产报损信息的总记录
     */
    public int getTotal()
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		int countTotal = (session.createQuery("from AssetDamageInfo").list()).size(); //资产信息的总数;
		
		trans.commit();
		
		return countTotal;
    }
    
    /*
     * 查询的资产信息的总记录
     */
    public int getTotalByQuery()
    {
    	return totalByQuery;
    }    
    
    @SuppressWarnings("unchecked")    
    /*
     * 通过参数得到资产的所有信息
     * 参数 start ， limit
     */
	public List getAllAssetBs(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();

		Query query = session.createQuery("from AssetDamageInfo");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		
		trans.commit();
		
		return l;
    } 
    
    @SuppressWarnings("unchecked")
    /*
     * 通过查询条件得到符合条件的资产信息
     * 参数 --- start ,limit ,assetNo , assetName , 
     *          useDepartment ,damageReason
     */
    public List getAssetBsByQuery(int start,int limit,String assetNo,String assetName,String useDepartment,String damageReason)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetDamageInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(assetName))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or name=:assetName ";
			}
			else
			{
				str = str + "name=:assetName ";
			}
		}
		if(!"".equals(useDepartment))
		{
			if(!"".equals(assetNo) || !"".equals(assetName))
			{
				str = str + "or department=:useDepartment ";
			}
			else
			{
				str = str + "department=:useDepartment ";
			}
		}
		if(!"".equals(damageReason))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(useDepartment))
			{
				str = str + "or damageReason=:damageReason ";
			}
			else
			{
				str = str + "damageReason=:damageReason ";
			}
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(assetName))
		{
			query.setString("assetName", assetName);
		}
		if(!"".equals(useDepartment))
		{
			query.setString("useDepartment", useDepartment);
		}		
		if(!"".equals(damageReason))
		{
			query.setString("damageReason", damageReason);
		}		
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产信息的记录总数
		
		query.setFirstResult(start);    //查询记录从这一条开始；
		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }    
    
    @SuppressWarnings("unchecked")
    /*
     * 通过资产编号（assetNo）删除资产信息
     */
	public void deleteAssetBs(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			AssetDamageInfo assetDamageInfo = (AssetDamageInfo)session.get(AssetDamageInfo.class, assetNo.elementAt(i).toString());
			session.delete(assetDamageInfo);
		}
		
		trans.commit();
    } 
    
    /*
     * 通过资产编号(assetNo)更新资产报损信息
     * 参数 -- 除了资产状态和资产编号其他都可以更新
     */
    public void updataAssetBs(String assetBs[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		Date AppraisalTime = java.sql.Date.valueOf(assetBs[5]);
		Date startUseTime = java.sql.Date.valueOf(assetBs[9]);
		double money = Double.parseDouble(assetBs[7]);
		int lifeYears = Integer.parseInt(assetBs[3]);		
		
    	AssetDamageInfo assetDamageInfo = (AssetDamageInfo)session.get(AssetDamageInfo.class, assetBs[0]); 
    	
		assetDamageInfo.setModel(assetBs[1]);
		assetDamageInfo.setDepartment(assetBs[2]);
		assetDamageInfo.setLifeYears(lifeYears);		
		assetDamageInfo.setAppraisaler(assetBs[4]);
		assetDamageInfo.setAppraisalTime(AppraisalTime);
		assetDamageInfo.setName(assetBs[6]);
		assetDamageInfo.setMoney(money);		
		assetDamageInfo.setPrincipal(assetBs[8]);
		assetDamageInfo.setStartUseTime(startUseTime);
		assetDamageInfo.setNowStatus(assetBs[10]);
		assetDamageInfo.setDamageReason(assetBs[11]);
		assetDamageInfo.setRemark(assetBs[12]);
		assetDamageInfo.setAppraisalOpinion(assetBs[13]);
		
		session.update(assetDamageInfo);
		
		trans.commit();
    }
}
